import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../generated/assets.dart';
import '../../utils/tools/keep_alive_wrapper.dart';
import '../home_main/logic.dart';
import '../home_main/view.dart';
import '../mine/mine_home/logic.dart';
import '../mine/mine_home/view.dart';
import '../work/work_home/logic.dart';
import '../work/work_home/view.dart';

class MainTabbarLogic extends GetxController {


  //页面控制器
  late final PageController pageController;

  //子控制器试图集合
  late final List<Widget> pageList;

  //底部选项集合
  late final List<Map<String, String>> tabbarList;
  //当前选中
  RxInt tabbarIndex = 0.obs;
  var message = "".obs;
  @override
  void onInit() {
    // TODO: implement onInit
    pageController = PageController(initialPage: 0);
    pageList = [
      KeepAliveWrapper(child:HomeMainPage()),
      KeepAliveWrapper(child: WorkHomePage()),
      KeepAliveWrapper(child: MineHomePage()),
    ];
    tabbarList = [
      {"label": "首页", "icon": Assets.iconHomeNo, "activeIcon": Assets.iconHomeSe},
      {"label": "工作", "icon": Assets.iconWorkNo, "activeIcon": Assets.iconWorkSe},
      {"label": "我的", "icon": Assets.iconMineNo, "activeIcon": Assets.iconMineSe},
    ];

    ever(message, (value) {
      print("收到消息: $value");
    });
    super.onInit();
  }

  void sendMessage(String text) {
    print("sendMessage收到消息: $text");
    message.value = text; // 触发通知
    jumpPage(0);
    refreshPage(0);
  }

  /*跳转页面*/
  void jumpPage(int index) {
    pageController.jumpToPage(index);
    tabbarIndex.value = index;
    WidgetsBinding.instance.addPostFrameCallback((_) {
      refreshPage(index);
    });
  }

  /* 根据索引刷新对应页面 */
  void refreshPage(int index) {
    switch (index) {
      case 0:
        Get.find<HomeMainLogic>().loadData();
        break;
      case 1:
        Get.find<WorkHomeLogic>().loadData();
        break;
      case 2:
        Get.find<MineHomeLogic>().loadData();
        break;

    }
  }

  @override
  void onClose() {
    // TODO: implement onClose
    pageController.dispose();
    super.onClose();
  }

}
